<?php

include dirname( __FILE__ )."/header.php";
include_once XOOPS_ROOT_PATH."/class/xoopstree.php";

$myts =& MyTextSanitizer::getInstance(); // MyTextSanitizer object
$mytree = new XoopsTree($xoopsDB->prefix($mydirname."_cat"),"cid","pid");
$xoopsOption['template_main'] = $mydirname.'_topten.html';
include XOOPS_ROOT_PATH."/header.php";
//wanikoo
$mydownloads_module_header = '';
$mydownloads_module_header .= '
	<link rel="stylesheet" type="text/css" href="'.mydownloadsGetStyleURL("mydownloads.css").'" />
	<script src="'.mydownloadsGetStyleURL("mydownloads.js").'" type="text/javascript"></script>
	';
$mydownloads_module_header = $mydownloads_module_header; 
$xoopsTpl->assign('xoops_module_header', $mydownloads_module_header);
//

//generates top 10 charts by rating and hits for each main category
if(!empty($_GET['rate'])){
    $sort = _MD_MYDOWNLOADS_RATING;
    $sortDB = "rating";
}else{
    $sort = _MD_MYDOWNLOADS_HITS;
    $sortDB = "hits";
}
$xoopsTpl->assign('lang_sortby' ,$sort);
$xoopsTpl->assign('lang_rank' , _MD_MYDOWNLOADS_RANK);
$xoopsTpl->assign('lang_title' , _MD_MYDOWNLOADS_TITLE);
$xoopsTpl->assign('lang_category' , _MD_MYDOWNLOADS_CATEGORY);
$xoopsTpl->assign('lang_hits' , _MD_MYDOWNLOADS_HITS);
$xoopsTpl->assign('lang_rating' , _MD_MYDOWNLOADS_RATING);
$xoopsTpl->assign('lang_vote' , _MD_MYDOWNLOADS_VOTE);
$arr=array();
$result=$xoopsDB->query("SELECT cid, title FROM ".$xoopsDB->prefix($mydirname."_cat")." WHERE pid=0");
$e = 0;
$rankings = array();
while(list($cid,$ctitle)=$xoopsDB->fetchRow($result)){
    $rankings[$e]['title'] = sprintf(_MD_MYDOWNLOADS_TOP10, $myts->htmlSpecialChars($ctitle));
    $query = "SELECT lid, cid, title, hits, rating, votes FROM ".$xoopsDB->prefix($mydirname."_downloads")." WHERE status>0 AND (cid=$cid";
// get all child cat ids for a given cat id
    $arr=$mytree->getAllChildId($cid);
    $size = count($arr);
    for($i=0;$i<$size;$i++){
        $query .= " or cid=".$arr[$i]."";
    }
    $query .= ") order by ".$sortDB." DESC";
    $result2 = $xoopsDB->query($query,10,0);
    $rank = 1;
    while(list($did,$dcid,$dtitle,$hits,$rating,$votes)=$xoopsDB->fetchRow($result2)){
        $catpath = $mytree->getPathFromId($dcid, "title");
        $catpath= substr($catpath, 1);
        $catpath = str_replace("/"," <span class='fg2'>&raquo;&raquo;</span> ",$catpath);
        $dtitle = $myts->makeTboxData4Show($dtitle);
        $rankings[$e]['file'][] = array('id' => $did, 'cid' => $dcid, 'rank' => $rank, 'title' => $dtitle, 'category' => $catpath, 'hits' => $hits, 'rating' => number_format($rating, 2), 'votes' => $votes);
        $rank++;
    }
    $e++;
}
$xoopsTpl->assign('rankings', $rankings);

//wanikoo theme changer
$xoopsTpl->assign("lang_themechanger", _MD_MYDOWNLOADS_THEMECHANGER);
$mymydownloadstheme_options = '';

    foreach ($GLOBALS['mydownloads_allowed_theme'] as $mymydownloadstheme) {
        $mymydownloadstheme_options .= '<option value="'.$mymydownloadstheme.'"';
        if ($mymydownloadstheme == $GLOBALS['mydownloads_theme']) {
            $mymydownloadstheme_options .= ' selected="selected"';
        }
        $mymydownloadstheme_options .= '>'.$mymydownloadstheme.'</option>';
    }

    $mydownloadstheme_select = '<select name="mydownloads_theme_select" onchange="submit();" size="1">'.$mymydownloadstheme_options.'</select>';

$xoopsTpl->assign("mydownloadsthemeoption", $mydownloadstheme_select);

//wanikoo search
if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php") ) {
   include_once XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php";
} else {
   include_once XOOPS_ROOT_PATH."/language/english/search.php";
}
$xoopsTpl->assign('lang_all', _SR_ALL);
$xoopsTpl->assign('lang_any', _SR_ANY);
$xoopsTpl->assign('lang_exact', _SR_EXACT);
$xoopsTpl->assign('lang_search', _SR_SEARCH);
$xoopsTpl->assign('module_id', $xoopsModule->getVar('mid'));

//category head
$catarray = array();
if ( $mydownloads_show_letters ) {
$catarray['letters'] = md_wfd_letters();
}
if ( $mydownloads_show_toolbar ) {
$catarray['toolbar'] = md_wfd_toolbar();
}
$xoopsTpl->assign('catarray', $catarray);

include_once dirname( __FILE__ )."/footer.php";

?>